package test.com.sms.selenium.singlepoint.util;

import org.openqa.selenium.By;

import test.com.sms.selenium.framework.SeleniumPageUtil;
import test.com.sms.selenium.util.SeleniumUtil;

/** enter page by Ticketing \ New Incident(Call Center) , create and save record*/
public class SpSearchPageUtil extends SeleniumPageUtil {
	
	public final static By SEARCH_LINK_SEARCH 			= By.id("ctl44_btnSearch");
	public final static By SEARCH_LINK_CLEAR			= By.id("ctl44_btnClear");
	public final static By SEARCH_TEXT_NORESULTSFOUND	= By.id("ctl44_lblMsg");
	public final static By SEARCH_LBL_INCIDENT_ID = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[1]/td[1]");
	public final static By SEARCH_INPUT_INCIDENT_NO 	= By.id("ctl44_txtIncidentID");
	public final static By SEARCH_LBL_DESCRIPTION = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[1]/td[3]");
	public final static By SEARCH_INPUT_DESCRIPTION = By.id("ctl44_txtDescription");
	public final static By SEARCH_LBL_PRORITY = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[1]/td[5]");
	public final static By SEARCH_SELECT_PRIORITY = By.id("ctl44_cboPriority");
	public final static By SEARCH_LBL_CUSTOMER_TICKET = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[2]/td[1]");
	public final static By SEARCH_INPUT_CUSTOMER_TICKET_NUMBER = By.id("ctl44_txtCustomerTicketNumber");
	
	// Status check boxes
	public final static By SEARCH_CHECK_STATUS_OPEN = By.id("ctl44_chkOpen");
	public final static By SEARCH_CHECK_STATUS_ASSIGNED = By.id("ctl44_chkAssigned");
	public final static By SEARCH_CHECK_STATUS_INPROGRESS = By.id("ctl44_chkInProgress");
	public final static By SEARCH_CHECK_STATUS_SCHEDULED = By.id("ctl44_chkScheduled");
	public final static By SEARCH_CHECK_STATUS_ESCALATED = By.id("ctl44_chkEscalated");
	public final static By SEARCH_CHECK_STATUS_CSG = By.id("ctl44_chkCSG");
	public final static By SEARCH_CHECK_STATUS_HOLD = By.id("ctl44_chkHold");
	public final static By SEARCH_CHECK_STATUS_RESOLVED = By.id("ctl44_chkResolved");
	public final static By SEARCH_CHECK_STATUS_CLOSED = By.id("ctl44_chkClosed");
	
	//check box labels
	public final static By SEARCH_LBL_STATUS = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[1]");
	public final static By SEARCH_LBL_STATUS_OPEN = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[1]");
	public final static By SEARCH_LBL_STATUS_ASSIGNED = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[2]");
	public final static By SEARCH_LBL_STATUS_IN_PROGRESS = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[3]");
	public final static By SEARCH_LBL_STATUS_SCHEDULED = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[4]");
	public final static By SEARCH_LBL_STATUS_ESCALATED = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[5]");
	public final static By SEARCH_LBL_STATUS_CSG = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[6]");
	public final static By SEARCH_LBL_STATUS_HOLD = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[7]");
	public final static By SEARCH_LBL_STATUS_RESOLVED = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[8]");
	public final static By SEARCH_LBL_STATUS_CLOSED = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[3]/td[2]/label[9]");
	
	// # of days
	public final static By SEARCH_RADIO_NA = By.id("ctl44_rdoNA");
	public final static By SEARCH_RADIO_0 = By.id("ctl44_rdo0");
	public final static By SEARCH_RADIO_1 = By.id("ctl44_rdo1");
	public final static By SEARCH_RADIO_30 = By.id("ctl44_rdo30");
	public final static By SEARCH_RADIO_60 = By.id("ctl44_rdo60");
	public final static By SEARCH_RADIO_90 = By.id("ctl44_rdo90");
	
	//# of days labels
	public final static By SEARCH_LBL_OF_DAYS = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[1]");
	public final static By SEARCH_LBL_OF_DAYS_NA = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[1]");
	public final static By SEARCH_LBL_OF_DAYS_TODAY = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[2]");
	public final static By SEARCH_LBL_OF_DAYS_1 = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[3]");
	public final static By SEARCH_LBL_OF_DAYS_30 = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[4]");
	public final static By SEARCH_LBL_OF_DAYS_60 = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[5]");
	public final static By SEARCH_LBL_OF_DAYS_90 = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[2]/label[6]");
	
	public final static By SEARCH_LBL_INCIDENT_DATE = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[3]");
	public final static By SEARCH_INPUT_STARTDATE_DATEPART = By.id("ctl44_dteStartDate_txtDatePart");
	public final static By SEARCH_LINK_INCIDENT_START_DATE = By.id("ctl44_dteStartDate_imgDateCalendar");
	public final static By SEARCH_INPUT_ENDDATE_DATEPART = By.id("ctl44_dteEndDate_txtDatePart");
	public final static By SEARCH_LINK_INCIDENT_END_DATE = By.id("ctl44_dteEndDate_imgDateCalendar");
	// incident date today's date link
	public final static By SEARCH_LINK_INCIDENT_END_DATE_TODAY = By.id("ctl44_dteEndDate_txtDatePart_CalendarExtender_today");

	public final static By SEARCH_LBL_CLOSE_DATE = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[4]/td[5]");
	public final static By SEARCH_INPUT_STARTCLOSEDATE_DATEPART = By.id("ctl44_dteStartCloseDate_txtDatePart");
	public final static By SEARCH_LINK_START_CLOSE_DATE = By.id("ctl44_dteStartCloseDate_imgDateCalendar");
	public final static By SEARCH_INPUT_ENDCLOSEDATE_DATEPART = By.id("ctl44_dteEndCloseDate_txtDatePart");
	public final static By SEARCH_LINK_END_CLOSE_DATE = By.id("ctl44_dteEndCloseDate_imgDateCalendar");
	// close date today's date link
	public final static By SEARCH_LINK_CLOSE_END_DATE_TODAY = By.id("ctl44_dteEndCloseDate_txtDatePart_CalendarExtender_today");
	
	// Customer input and select
	public final static By SEARCH_LBL_CUSTOMER = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[5]/td[1]");
	public final static By SEARCH_INPUT_CUSTOMER = By.id("ctl44_txtCustomer");
	public final static By SEARCH_DIV_SELECTION_CUSTOMER= By.id("ctl44_aceCustomer_completionListElem");
	
	
	public final static By SEARCH_LBL_SHIP_TO_ADDRESS = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[5]/td[3]");
	public final static By SEARCH_INPUT_ADDRESS = By.id("ctl44_txtAddress");
	public final static By SEARCH_LBL_CONTACT = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[6]/td[1]");
	public final static By SEARCH_INPUT_CONTACT = By.id("ctl44_txtContact");
	public final static By SEARCH_LBL_REQUESTER = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[6]/td[3]");
	public final static By SEARCH_INPUT_REQUESTER = By.id("ctl44_txtRequester");
	public final static By SEARCH_LBL_CREATED_BY = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[6]/td[5]");
	public final static By SEARCH_DIV_SELECTION_CREATED_BY = By.id("ctl44_aceCreatedBy_completionListElem");
	public final static By SEARCH_INPUT_CREATEDBY = By.id("ctl44_txtCreatedBy");
	public final static By SEARCH_LBL_UNIT_ID = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[7]/td[1]");
	public final static By SEARCH_INPUT_SERIALNUMBER = By.id("ctl44_txtSerialNumber");
	// PE/Owner input and select
	public final static By SEARCH_LBL_FE_OWNER = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[7]/td[3]");
	public final static By SEARCH_INPUT_FE_OWNER = By.id("ctl44_txtAssignedTo");
	public final static By SEARCH_DIV_SELECTION_FE_OWNER = By.id("ctl44_aceAssignedTo_completionListElem");
	
	public final static By SEARCH_LBL_CONTRACT = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[7]/td[5]");
	public final static By SEARCH_INPUT_CONTRACT = By.id("ctl44_txtContract");
	public final static By SEARCH_DIV_SELECTION_CONTRACT = By.id("ctl44_aceContract_completionListElem");	
	public final static By SEARCH_LBL_SRO = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[8]/td[1]");
	public final static By SEARCH_INPUT_SRO = By.id("ctl44_txtSRO");
	public final static By SEARCH_LBL_SRO_STATUS = By.xpath("//*[@id='ctl44_pnlDefault']/table[2]/tbody/tr[8]/td[3]");
	public final static By SEARCH_SELECT_SRO_STATUS 	= By.id("ctl44_cboSROStat");
	
	// Search result table 
	public final static By SEARCH_TBL_RESULT      = By.id("tblResults");
	
	public final static By SEARCH_LINK_INCIDENT      = By.xpath("//*[@id='tblResults']/tbody/tr[6]/td[1]/a");
	public final static By SEARCH_TD_RESULT_COUNT = By.xpath("//*[@id='ctl44_lblResults']/table[2]/tbody/tr/td[2]");
	public final static By SEARCH_RESULT_CONTRACT = By.id("tblResults");
	
	// first record of search results
	public final static By SEARCH_LINK_INCIDENT_ID = By.xpath("//*[@id='tblResults']//a[1]");
	
	// element of open one record.
	public final static By SEARCH_INPUT_ASSIGNED_TO = By.id("IncidentHdr1_txtAssignedTo");
	public final static By SEARCH_BUTTON_SAVE = By.xpath("//*[@id='badd6d10-5932-4bbc-b11c-c04a27fe36ac']/a");
	public final static By SEARCH_TD_SRO = By.id("tab9");
	public final static By SEARCH_LINK_SRO_SAVE = By.xpath("//*[@id='0476f4c1-4ba9-4f14-9f2b-01f0086d51cd']/a");
	public final static By SEARCH_INPUT_SRO_ASSIGNED_TO = By.id("SRO_txtAssignedTo");
	
	
	// list of assigned to when input 'Zill'.
	public final static By SEARCH_LI_ASSIGNED_TO = By.xpath("//*[@id='IncidentHdr1_aceAssignedTo_completionListElem']/li");
	
	public static void enter(){
		SpHomePageUtil.enter();
		SpHomePageUtil.enterSearchPage();
	}
	
	public static void waitForPageToLoad(){
		// search page in content area
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.waitOnElements(new By[]{
				SEARCH_LINK_SEARCH, SEARCH_LINK_CLEAR, SEARCH_INPUT_INCIDENT_NO, SEARCH_SELECT_SRO_STATUS
			});
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SEARCH_LINK_SEARCH));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SEARCH_LINK_CLEAR));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SEARCH_INPUT_INCIDENT_NO));
		SeleniumUtil.isDisplayed(SeleniumUtil.findElement(SEARCH_SELECT_SRO_STATUS));
	}
	
	/** wait for result list to show all records, wait for 6 rounds at most, and 
	 * each round 0.5 seconds. */
	public static void waitForResultListToLoadComplete(){
		
		final int WAIT_FOR_ROUND = 6;
		final int WAIT_FOR_MILLISECONDS = 500;
		
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		// if result list shows, wait for all records to load
		long prevCount = 0;
		if(SeleniumUtil.findElements(SEARCH_TBL_RESULT).size() > 0){
			int stableRound = 0;
			while(true){
				long beforeFind = System.currentTimeMillis();
				long rowCount = SeleniumUtil.findElements(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_TBL_RESULT), By.xpath(".//a")).size();
				long endFind = System.currentTimeMillis();
				
				// make sure not wait too long when really no result exists
				if(endFind - beforeFind >= WAIT_FOR_ROUND * WAIT_FOR_MILLISECONDS) {
					break;
				}
				
				// if record count becomes stable, quit wait, else continue 
				if(rowCount == prevCount){
					stableRound ++;
					if(stableRound == WAIT_FOR_ROUND) break;
				} else {
					stableRound = 0;
				}
				prevCount = rowCount;
				SeleniumUtil.waitForFixedMilliseconds(WAIT_FOR_MILLISECONDS);
			}
		}
	}
	
	public static void searchByIncidentId(String incidentId) {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.findElement(SEARCH_INPUT_INCIDENT_NO), incidentId);
		SeleniumUtil.click(SeleniumUtil.findElement(SEARCH_LINK_SEARCH));
	}
	
	
	public static void inputAndSelectCustomer(String inputText, String selectText) {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_INPUT_CUSTOMER), inputText);
		// wait for customer list show
		SeleniumUtil.waitPageToLoad(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CUSTOMER));
		SeleniumUtil.click(SeleniumUtil.findElement(
				SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CUSTOMER), By.xpath(".//td[text()='" + selectText + "']")));
	}
	
	public static void inputAndSelectFEOwner(String inputText, String selectText){
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_INPUT_FE_OWNER), inputText);
		// wait for PE/Owner list show
		SeleniumUtil.waitPageToLoad(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_FE_OWNER));
		SeleniumUtil.click(SeleniumUtil.findElement(
				SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_FE_OWNER), By.xpath(".//li[text()='" + selectText + "']")));
	}
	
	public static void inputAndSelectCreatedBy(String inputText, String selectText){
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_INPUT_CREATEDBY), inputText);
		// wait for CreatedBy list show
		SeleniumUtil.waitPageToLoad(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CREATED_BY));
		SeleniumUtil.click(SeleniumUtil.findElement(
				SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CREATED_BY), By.xpath(".//li[text()='" + selectText + "']")));
	}
	
	public static void inputAndSelectContract(String inputText, String selectText) {
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		SeleniumUtil.sendKeys(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_INPUT_CONTRACT), inputText);
		// wait for customer list show
		SeleniumUtil.waitPageToLoad(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CONTRACT));
		SeleniumUtil.click(SeleniumUtil.findElement(
				SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_DIV_SELECTION_CONTRACT), By.xpath(".//li[text()='" + selectText + "']")));
	}
	
	/** un-check all status check boxes*/
	public static void deSelectAllStatus(){
		SeleniumUtil.selectDefaultContent();
		SeleniumUtil.selectFrame(SpHomePageUtil.HOME_FRAME_CONTENT_NAME);
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_OPEN))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_OPEN));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_ASSIGNED))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_ASSIGNED));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_INPROGRESS))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_INPROGRESS));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_SCHEDULED))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_SCHEDULED));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_ESCALATED))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_ESCALATED));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_CSG))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_CSG));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_HOLD))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_HOLD));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_RESOLVED))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_RESOLVED));
		}
		if (SeleniumUtil.isSelected(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_CLOSED))) {
			SeleniumUtil.click(SeleniumUtil.getCachedElement(SpSearchPageUtil.SEARCH_CHECK_STATUS_CLOSED));
		}
	}

}
